﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Encryption.models
{
    /// <summary>
    /// 加密类
    /// </summary>
    internal class Encodr
    {
        //3个属性对应明文密文和密钥
        /// <summary>
        /// 明文
        /// </summary>
        public string Plaintext { get; set; }

        /// <summary>
        /// 密钥
        /// </summary>
        public string Key { get; set; }

        /// <summary>
        /// 密文
        /// </summary>
        public string Cypher { get; set; }

        /// <summary>
        /// 加密
        /// </summary>
        public void Encoder()
        {
            //加密过程
            for (int i = 0, j = 0; i < Plaintext.Length; i++, j++)
            {
                //超过密钥长度部分循环
                if (j % Key.Length == 0)
                    j = 0;
                //计算密文
                int num = (Function(Plaintext[i]) + Function(Key[j])) % 26;
                Cypher += Convert.ToChar(num + 'a');
            }

        }

        /// <summary>
        /// 返回指定字符在字母表中序号，从0开始
        /// </summary>
        /// <param name="ch"></param>
        /// <returns></returns>
        private int Function(char ch)
        {
            return ch - 'a';
        }
    }
}
